package com.lib.application;

import com.lib.model.Customer;
import com.lib.model.WxAccount;
import com.lib.repository.CustomerRepository;
import com.lib.repository.RentalAgentRepository;
import com.lib.repository.WxAccountReponsitory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by duqingxiang on 17/5/23.
 */
@Service
public class CustomerService {

    @Autowired
    CustomerRepository customerRepository;
    @Autowired
    WxAccountReponsitory wxAccountReponsitory;

    public void registerCustomer(WxAccount account) {

        Customer customer = Customer.builder()
                .wxAccount(account)
                .createdDate(new Date())
                .build();
        customerRepository.save(customer);

    }

    public Customer getCustomerByOpenId(String openId) {
        WxAccount wxAccount = wxAccountReponsitory.findWxAccountByOpenId(openId);
        if (wxAccount == null) {
            return null;
        }
        return wxAccount.getCustomer();
    }


}
